<?php
namespace app\index\controller;
use app\index\middleware\Auth;
use app\libs\Predis;
use think\Config;
use think\Controller;
use think\Request;
use app\index\business\BusinessFactory;
class BaseController extends Controller
{
    protected $cloud = '';
    protected $redis = null;

    public $result = array(
        'status' => 1,
        'msg' => '',
        'data' => []
    );

    protected $beforeActionList = [
        'authorization'
    ];

    public function __construct(Request $request = null)
    {
        $this->cloud = Config::get('cloude_data.url');
        $this->redis = new Predis();
        parent::__construct($request);
    }

    protected function authorization()
    {
        $auth = new Auth();
        $authInfo = $auth->beforeExecuteRoute();
        if ($authInfo['status'] != 1 && $authInfo !== true) {
            $status = $authInfo['status'];
            $msg = $authInfo['msg'];
            $data = $authInfo['data'];
            die($this->json(compact('status','msg','data')));
        }
    }
    /**
     * ajax json 输出
     * array $result
     * 一般由个的元素组成'code','msg','data'
     */
    protected function ajaxReturn($result)
    {
        $this->response->setJsonContent($result);
        $this->response->send();
        $this->view->disable();
        exit();
    }

    /**
     * 获取业务层对象
     * @param type $businessName
     * @return object
     */
    protected function getBusiness($businessName){
        return BusinessFactory::getBusiness($businessName);
    }

    //返回json数据
    public function json($re)
    {
        if($re){
            header('Content-type: application/json');
            exit(json_encode($re,JSON_UNESCAPED_UNICODE));
        }else{
            exit('response data not correct!');
        }
    }
}